﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Web;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.ServiceModel.Activation;
using System.Text;
using HotelManagerService.Interface;

namespace HotelManagerService
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and config file together.
    // NOTE: In order to launch WCF Test Client for testing this service, please select Service1.svc or Service1.svc.cs at the Solution Explorer and start debugging.
    public class Service1 :INhanVien,  IBacLuong, IChucVu,IHinhThucThue, ILoaiTaiKhoan,IPhongBan
    {
        #region [NHANVIEN]

        public IEnumerable<NHANVIEN> GetByAllNhanVien()
        {
            try
            {
                using (var dataContext = new DataHotelLinQDataContext())
                {
                    var result = dataContext.NHANVIENs.ToList();
                    return result;
                }

            }
            catch (Exception)
            {
                return null;
            }
        }

        public string InsertNhanVien(NHANVIEN data)
        {
            try
            {
                using (var dataContext = new DataHotelLinQDataContext())
                {
                    //Check existed
                    var existItem = (from item in dataContext.NHANVIENs where item.MaNhanVien.Equals(data.MaNhanVien) select item).FirstOrDefault();
                   
                    var result = (from item in dataContext.NHANVIENs
                                  orderby item.MaNhanVien descending
                                  select item).FirstOrDefault();
                    if (result != null)
                    {
                        string id = result.MaNhanVien;
                        data.MaNhanVien = (int.Parse(id.Replace("NV", "")) + 1).ToString("NV0");
                    }
                    else
                        data.MaNhanVien = "NV1";

                    dataContext.NHANVIENs.InsertOnSubmit(data);
                    dataContext.SubmitChanges();
                    return "success";
                }
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }

        public string UpdateNhanVien(NHANVIEN data)
        {
            try
            {
                using (var dataContext = new DataHotelLinQDataContext())
                {
                    var result = (from item in dataContext.NHANVIENs
                                  where item.MaNhanVien.Equals(data.MaNhanVien)
                                  select item).SingleOrDefault();
                    result.Anh = data.Anh;
                    result.CMND = data.CMND;
                    result.HoTen = data.HoTen;
                    result.MaBacLuong = data.MaBacLuong;
                    result.MaChucVu = data.MaChucVu;
                    result.MaLoaiTaiKhoan = data.MaLoaiTaiKhoan;
                    result.MaPhongBan = data.MaPhongBan;
                    result.MatKhau = data.MatKhau;
                    result.NgayKiHopDong = data.NgayKiHopDong;
                    result.NgaySinh = data.NgaySinh;
                    result.SoHopDong = data.SoHopDong;
                    dataContext.SubmitChanges();
                    return "success";
                }
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }

        public string DeleteNhanVien(string id)
        {
            try
            {
                using (var dataContext = new DataHotelLinQDataContext())
                {
                    var result = (from item in dataContext.NHANVIENs
                                  where item.MaNhanVien.Equals(id)
                                  select item).SingleOrDefault();
                    dataContext.NHANVIENs.DeleteOnSubmit(result);
                    dataContext.SubmitChanges();
                    return "success";
                }
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }

        public IEnumerable<NHANVIEN> GetByTopNhanVien(string _top, string _where, string _order)
        {
            try
            {
                using (var data = new DataHotelLinQDataContext())
                {
                    string query = "Select * from [NHANVIEN]";
                    if (!_top.Equals(""))
                        query = " Select top " + _top + " * from [NHANVIEN] ";
                    if (!_where.Equals(""))
                        query += " where " + _where + " ";
                    if (!_order.Equals(""))
                        query += " order by " + _order;

                    IEnumerable<NHANVIEN> results = data.ExecuteQuery<NHANVIEN>(@query);

                    return results.ToList();
                }
            }
            catch (Exception)
            {
                return null;
            }
        }

        public string ApproveNhanVien(NHANVIEN data)
        {
            try
            {
                using (var dataContext = new DataHotelLinQDataContext())
                {

                    //update LNTYPE
                    var result = (from current in dataContext.NHANVIENs where current.MaNhanVien == data.MaNhanVien select current).Single();

                  
                    dataContext.SubmitChanges();
                    return "success";
                }
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }

        #endregion
        #region [BACLUONG]

        public IEnumerable<BACLUONG> GetByAllBacLuong()
        {
            try
            {
                using (var dataContext = new DataHotelLinQDataContext())
                {
                    var result = dataContext.BACLUONGs.ToList();
                    return result;
                }

            }
            catch (Exception)
            {
                return null;
            }
        }

        public string InsertBacLuong(BACLUONG data)
        {
            try
            {
                using (var dataContext = new DataHotelLinQDataContext())
                {
                    //Check existed
                    var existItem = (from item in dataContext.BACLUONGs where item.MaBacLuong.Equals(data.MaBacLuong) select item).FirstOrDefault();

                    var result = (from item in dataContext.BACLUONGs
                                  orderby item.MaBacLuong descending
                                  select item).FirstOrDefault();
                    if (result != null)
                    {
                        string id = result.MaBacLuong;
                        data.MaBacLuong = (int.Parse(id.Replace("BL", "")) + 1).ToString("BL0");
                    }
                    else
                        data.MaBacLuong = "BL1";

                    dataContext.BACLUONGs.InsertOnSubmit(data);
                    dataContext.SubmitChanges();
                    return "success";
                }
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }

        public string UpdateBacLuong(BACLUONG data)
        {
            try
            {
                using (var dataContext = new DataHotelLinQDataContext())
                {
                    var result = (from item in dataContext.BACLUONGs
                                  where item.MaBacLuong.Equals(data.MaBacLuong)
                                  select item).SingleOrDefault();
                    result.HeSo = data.HeSo;
                    result.MaBacLuong = data.MaBacLuong;
                    dataContext.SubmitChanges();
                    return "success";
                }
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }

        public string DeleteBacLuong(string id)
        {
            try
            {
                using (var dataContext = new DataHotelLinQDataContext())
                {
                    var result = (from item in dataContext.BACLUONGs
                                  where item.MaBacLuong.Equals(id)
                                  select item).SingleOrDefault();
                    dataContext.BACLUONGs.DeleteOnSubmit(result);
                    dataContext.SubmitChanges();
                    return "success";
                }
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }

        public IEnumerable<BACLUONG> GetByTopBacLuong(string _top, string _where, string _order)
        {
            try
            {
                using (var data = new DataHotelLinQDataContext())
                {
                    string query = "Select * from [BACLUONG]";
                    if (!_top.Equals(""))
                        query = " Select top " + _top + " * from [BACLUONG] ";
                    if (!_where.Equals(""))
                        query += " where " + _where + " ";
                    if (!_order.Equals(""))
                        query += " order by " + _order;

                    IEnumerable<BACLUONG> results = data.ExecuteQuery<BACLUONG>(@query);

                    return results.ToList();
                }
            }
            catch (Exception )
            {
                return null;
            }
        }

        public string ApproveBacLuong(BACLUONG data)
        {
            try
            {
                using (var dataContext = new DataHotelLinQDataContext())
                {

                    //update LNTYPE
                    var result = (from current in dataContext.BACLUONGs where current.MaBacLuong == data.MaBacLuong select current).Single();


                    dataContext.SubmitChanges();
                    return "success";
                }
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }

        #endregion
        #region [CHUCVU]

        public IEnumerable<CHUCVU> GetByAllChucVu()
        {
            try
            {
                using (var dataContext = new DataHotelLinQDataContext())
                {
                    var result = dataContext.CHUCVUs.ToList();
                    return result;
                }

            }
            catch (Exception)
            {
                return null;
            }
        }

        public string InsertChucVu(CHUCVU data)
        {
            try
            {
                using (var dataContext = new DataHotelLinQDataContext())
                {
                    //Check existed
                    var existItem = (from item in dataContext.CHUCVUs where item.MaChucVu.Equals(data.MaChucVu) select item).FirstOrDefault();

                    var result = (from item in dataContext.CHUCVUs
                                  orderby item.MaChucVu descending
                                  select item).FirstOrDefault();
                    if (result != null)
                    {
                        string id = result.MaChucVu;
                        data.MaChucVu = (int.Parse(id.Replace("CV", "")) + 1).ToString("CV0");
                    }
                    else
                        data.MaChucVu = "CV1";

                    dataContext.CHUCVUs.InsertOnSubmit(data);
                    dataContext.SubmitChanges();
                    return "success";
                }
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }

        public string UpdateChucVu(CHUCVU data)
        {
            try
            {
                using (var dataContext = new DataHotelLinQDataContext())
                {
                    var result = (from item in dataContext.CHUCVUs
                                  where item.MaChucVu.Equals(data.MaChucVu)
                                  select item).SingleOrDefault();
                    result.MaPhuCap = data.MaPhuCap;
                    result.TenChucVu = data.TenChucVu;
                    dataContext.SubmitChanges();
                    return "success";
                }
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }

        public string DeleteChucVu(string id)
        {
            try
            {
                using (var dataContext = new DataHotelLinQDataContext())
                {
                    var result = (from item in dataContext.CHUCVUs
                                  where item.MaChucVu.Equals(id)
                                  select item).SingleOrDefault();
                    dataContext.CHUCVUs.DeleteOnSubmit(result);
                    dataContext.SubmitChanges();
                    return "success";
                }
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }

        public IEnumerable<CHUCVU> GetByTopChucVu(string _top, string _where, string _order)
        {
            try
            {
                using (var data = new DataHotelLinQDataContext())
                {
                    string query = "Select * from [CHUCVU]";
                    if (!_top.Equals(""))
                        query = " Select top " + _top + " * from [CHUCVU] ";
                    if (!_where.Equals(""))
                        query += " where " + _where + " ";
                    if (!_order.Equals(""))
                        query += " order by " + _order;

                    IEnumerable<CHUCVU> results = data.ExecuteQuery<CHUCVU>(@query);

                    return results.ToList();
                }
            }
            catch (Exception)
            {
                return null;
            }
        }

        public string ApproveChucVu(CHUCVU data)
        {
            try
            {
                using (var dataContext = new DataHotelLinQDataContext())
                {

                    //update LNTYPE
                    var result = (from current in dataContext.CHUCVUs where current.MaChucVu == data.MaChucVu select current).Single();


                    dataContext.SubmitChanges();
                    return "success";
                }
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }

        #endregion
        #region [LOAITAIKHOAN]

        public IEnumerable<LOAITAIKHOAN> GetByAllLoaiTaiKhoan()
        {
            try
            {
                using (var dataContext = new DataHotelLinQDataContext())
                {
                    var result = dataContext.LOAITAIKHOANs.ToList();
                    return result;
                }

            }
            catch (Exception)
            {
                return null;
            }
        }

        public string InsertLoaiTaiKhoan(LOAITAIKHOAN data)
        {
            try
            {
                using (var dataContext = new DataHotelLinQDataContext())
                {
                    //Check existed
                    var existItem = (from item in dataContext.LOAITAIKHOANs where item.MaLoaiTaiKhoan.Equals(data.MaLoaiTaiKhoan) select item).FirstOrDefault();

                    var result = (from item in dataContext.LOAITAIKHOANs
                                  orderby item.MaLoaiTaiKhoan descending
                                  select item).FirstOrDefault();
                    if (result != null)
                    {
                        string id = result.MaLoaiTaiKhoan;
                        data.MaLoaiTaiKhoan = (int.Parse(id.Replace("LTK", "")) + 1).ToString("LTK0");
                    }
                    else
                        data.MaLoaiTaiKhoan = "LTK1";

                    dataContext.LOAITAIKHOANs.InsertOnSubmit(data);
                    dataContext.SubmitChanges();
                    return "success";
                }
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }

        public string UpdateLoaiTaiKhoan(LOAITAIKHOAN data)
        {
            try
            {
                using (var dataContext = new DataHotelLinQDataContext())
                {
                    var result = (from item in dataContext.LOAITAIKHOANs
                                  where item.MaLoaiTaiKhoan.Equals(data.MaLoaiTaiKhoan)
                                  select item).SingleOrDefault();
                    result.TenLoaiTaiKhoan = data.TenLoaiTaiKhoan;
                    dataContext.SubmitChanges();
                    return "success";
                }
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }

        public string DeleteLoaiTaiKhoan(string id)
        {
            try
            {
                using (var dataContext = new DataHotelLinQDataContext())
                {
                    var result = (from item in dataContext.LOAITAIKHOANs
                                  where item.MaLoaiTaiKhoan.Equals(id)
                                  select item).SingleOrDefault();
                    dataContext.LOAITAIKHOANs.DeleteOnSubmit(result);
                    dataContext.SubmitChanges();
                    return "success";
                }
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }

        public IEnumerable<LOAITAIKHOAN> GetByTopLoaiTaiKhoan(string _top, string _where, string _order)
        {
            try
            {
                using (var data = new DataHotelLinQDataContext())
                {
                    string query = "Select * from [LOAITAIKHOAN]";
                    if (!_top.Equals(""))
                        query = " Select top " + _top + " * from [LOAITAIKHOAN] ";
                    if (!_where.Equals(""))
                        query += " where " + _where + " ";
                    if (!_order.Equals(""))
                        query += " order by " + _order;

                    IEnumerable<LOAITAIKHOAN> results = data.ExecuteQuery<LOAITAIKHOAN>(@query);

                    return results.ToList();
                }
            }
            catch (Exception)
            {
                return null;
            }
        }

        public string ApproveLoaiTaiKhoan(LOAITAIKHOAN data)
        {
            try
            {
                using (var dataContext = new DataHotelLinQDataContext())
                {

                    //update LNTYPE
                    var result = (from current in dataContext.LOAITAIKHOANs where current.MaLoaiTaiKhoan == data.MaLoaiTaiKhoan select current).Single();


                    dataContext.SubmitChanges();
                    return "success";
                }
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }

        #endregion
        #region [PHONGBAN]

        public IEnumerable<PHONGBAN> GetByAllPhongBan()
        {
            try
            {
                using (var dataContext = new DataHotelLinQDataContext())
                {
                    var result = dataContext.PHONGBANs.ToList();
                    return result;
                }

            }
            catch (Exception)
            {
                return null;
            }
        }

        public string InsertPhongBan(PHONGBAN data)
        {
            try
            {
                using (var dataContext = new DataHotelLinQDataContext())
                {
                    //Check existed
                    var existItem = (from item in dataContext.PHONGBANs where item.MaPhongBan.Equals(data.MaPhongBan) select item).FirstOrDefault();

                    var result = (from item in dataContext.PHONGBANs
                                  orderby item.MaPhongBan descending
                                  select item).FirstOrDefault();
                    if (result != null)
                    {
                        string id = result.MaPhongBan;
                        data.MaPhongBan = (int.Parse(id.Replace("BL", "")) + 1).ToString("PB0");
                    }
                    else
                        data.MaPhongBan = "PB1";

                    dataContext.PHONGBANs.InsertOnSubmit(data);
                    dataContext.SubmitChanges();
                    return "success";
                }
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }

        public string UpdatePhongBan(PHONGBAN data)
        {
            try
            {
                using (var dataContext = new DataHotelLinQDataContext())
                {
                    var result = (from item in dataContext.PHONGBANs
                                  where item.MaPhongBan.Equals(data.MaPhongBan)
                                  select item).SingleOrDefault();
                    result.MaPhong = data.MaPhong;
                    result.TenPhongBan = data.TenPhongBan;
                    dataContext.SubmitChanges();
                    return "success";
                }
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }

        public string DeletePhongBan(string id)
        {
            try
            {
                using (var dataContext = new DataHotelLinQDataContext())
                {
                    var result = (from item in dataContext.PHONGBANs
                                  where item.MaPhongBan.Equals(id)
                                  select item).SingleOrDefault();
                    dataContext.PHONGBANs.DeleteOnSubmit(result);
                    dataContext.SubmitChanges();
                    return "success";
                }
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }

        public IEnumerable<PHONGBAN> GetByTopPhongBan(string _top, string _where, string _order)
        {
            try
            {
                using (var data = new DataHotelLinQDataContext())
                {
                    string query = "Select * from [PHONGBAN]";
                    if (!_top.Equals(""))
                        query = " Select top " + _top + " * from [PHONGBAN] ";
                    if (!_where.Equals(""))
                        query += " where " + _where + " ";
                    if (!_order.Equals(""))
                        query += " order by " + _order;

                    IEnumerable<PHONGBAN> results = data.ExecuteQuery<PHONGBAN>(@query);

                    return results.ToList();
                }
            }
            catch (Exception)
            {
                return null;
            }
        }

        public string ApprovePhongBan(PHONGBAN data)
        {
            try
            {
                using (var dataContext = new DataHotelLinQDataContext())
                {

                    //update LNTYPE
                    var result = (from current in dataContext.PHONGBANs where current.MaPhongBan == data.MaPhongBan select current).Single();


                    dataContext.SubmitChanges();
                    return "success";
                }
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }

        #endregion
        #region [HINHTHUCTHUE]

        public IEnumerable<HINHTHUCTHUE> GetByAllHinhThucThue()
        {
            try
            {
                using (var dataContext = new DataHotelLinQDataContext())
                {
                    var result = dataContext.HINHTHUCTHUEs.ToList();
                    return result;
                }

            }
            catch (Exception)
            {
                return null;
            }
        }

        public string InsertHinhThucThue(HINHTHUCTHUE data)
        {
            try
            {
                using (var dataContext = new DataHotelLinQDataContext())
                {
                    //Check existed
                    var existItem = (from item in dataContext.HINHTHUCTHUEs where item.Equals(data.MaHinhThucThue) select item).FirstOrDefault();

                    var result = (from item in dataContext.HINHTHUCTHUEs
                                  orderby item.MaHinhThucThue descending
                                  select item).FirstOrDefault();
                    if (result != null)
                    {
                        string id = result.MaHinhThucThue;
                        data.MaHinhThucThue = (int.Parse(id.Replace("HTT", "")) + 1).ToString("HTT0");
                    }
                    else
                        data.MaHinhThucThue = "HTT1";

                    dataContext.HINHTHUCTHUEs.InsertOnSubmit(data);
                    dataContext.SubmitChanges();
                    return "success";
                }
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }

        public string UpdateHinhThucThue(HINHTHUCTHUE data)
        {
            try
            {
                using (var dataContext = new DataHotelLinQDataContext())
                {
                    var result = (from item in dataContext.HINHTHUCTHUEs
                                  where item.MaHinhThucThue.Equals(data.MaHinhThucThue)
                                  select item).SingleOrDefault();
                    result.SoGio = data.SoGio;
                    result.TenHinhThucThue = data.TenHinhThucThue;
                    result.TiLeDonGia = data.TiLeDonGia;
                    dataContext.SubmitChanges();
                    return "success";
                }
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }

        public string DeleteHinhThucThue(string id)
        {
            try
            {
                using (var dataContext = new DataHotelLinQDataContext())
                {
                    var result = (from item in dataContext.HINHTHUCTHUEs
                                  where item.MaHinhThucThue.Equals(id)
                                  select item).SingleOrDefault();
                    dataContext.HINHTHUCTHUEs.DeleteOnSubmit(result);
                    dataContext.SubmitChanges();
                    return "success";
                }
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }

        public IEnumerable<HINHTHUCTHUE> GetByTopHinhThucThue(string _top, string _where, string _order)
        {
            try
            {
                using (var data = new DataHotelLinQDataContext())
                {
                    string query = "Select * from [HINHTHUCTHUE]";
                    if (!_top.Equals(""))
                        query = " Select top " + _top + " * from [HINHTHUCTHUE] ";
                    if (!_where.Equals(""))
                        query += " where " + _where + " ";
                    if (!_order.Equals(""))
                        query += " order by " + _order;

                    IEnumerable<HINHTHUCTHUE> results = data.ExecuteQuery<HINHTHUCTHUE>(@query);

                    return results.ToList();
                }
            }
            catch (Exception)
            {
                return null;
            }
        }

        public string ApproveHinhThucThue(HINHTHUCTHUE data)
        {
            try
            {
                using (var dataContext = new DataHotelLinQDataContext())
                {

                    //update LNTYPE
                    var result = (from current in dataContext.HINHTHUCTHUEs where current.MaHinhThucThue == data.MaHinhThucThue select current).Single();


                    dataContext.SubmitChanges();
                    return "success";
                }
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }

        #endregion
        #region [BCDOANHTHU]

        public IEnumerable<BCDOANHTHU> GetByAllBCDoanhThu()
        {
            try
            {
                using (var dataContext = new DataHotelLinQDataContext())
                {
                    var result = dataContext.BCDOANHTHUs.ToList();
                    return result;
                }

            }
            catch (Exception)
            {
                return null;
            }
        }

        public string InsertBCDoanhThu(BCDOANHTHU data)
        {
            try
            {
                using (var dataContext = new DataHotelLinQDataContext())
                {
                    //Check existed
                    var existItem = (from item in dataContext.BCDOANHTHUs where item.MaBCDT.Equals(data.MaBCDT) select item).FirstOrDefault();

                    var result = (from item in dataContext.BCDOANHTHUs
                                  orderby item.MaBCDT descending
                                  select item).FirstOrDefault();
                    if (result != null)
                    {
                        string id = result.MaBCDT;
                        data.MaBCDT = (int.Parse(id.Replace("BCDT", "")) + 1).ToString("BCDT0");
                    }
                    else
                        data.MaBCDT = "BCDT1";

                    dataContext.BCDOANHTHUs.InsertOnSubmit(data);
                    dataContext.SubmitChanges();
                    return "success";
                }
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }

        public string UpdateBCDoanhThu(BCDOANHTHU data)
        {
            try
            {
                using (var dataContext = new DataHotelLinQDataContext())
                {
                    var result = (from item in dataContext.BCDOANHTHUs
                                  where item.MaBCDT.Equals(data.MaBCDT)
                                  select item).SingleOrDefault();
                    result.Nam = data.Nam;
                    result.Thang = data.Thang;
                    result.TongDoanhThu = data.TongDoanhThu;
                    dataContext.SubmitChanges();
                    return "success";
                }
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }

        public string DeleteBCDoanhThu(string id)
        {
            try
            {
                using (var dataContext = new DataHotelLinQDataContext())
                {
                    var result = (from item in dataContext.BCDOANHTHUs
                                  where item.MaBCDT.Equals(id)
                                  select item).SingleOrDefault();
                    dataContext.BCDOANHTHUs.DeleteOnSubmit(result);
                    dataContext.SubmitChanges();
                    return "success";
                }
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }

        public IEnumerable<BCDOANHTHU> GetByTopBCDoanhThu(string _top, string _where, string _order)
        {
            try
            {
                using (var data = new DataHotelLinQDataContext())
                {
                    string query = "Select * from [BCDOANHTHU]";
                    if (!_top.Equals(""))
                        query = " Select top " + _top + " * from [BCDOANHTHU] ";
                    if (!_where.Equals(""))
                        query += " where " + _where + " ";
                    if (!_order.Equals(""))
                        query += " order by " + _order;

                    IEnumerable<BCDOANHTHU> results = data.ExecuteQuery<BCDOANHTHU>(@query);

                    return results.ToList();
                }
            }
            catch (Exception)
            {
                return null;
            }
        }

        public string ApproveBCDoanhThu(BCDOANHTHU data)
        {
            try
            {
                using (var dataContext = new DataHotelLinQDataContext())
                {

                    //update LNTYPE
                    var result = (from current in dataContext.BCDOANHTHUs where current.MaBCDT == data.MaBCDT select current).Single();


                    dataContext.SubmitChanges();
                    return "success";
                }
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }

        #endregion
        #region [DICHVU]

        public IEnumerable<DICHVU> GetByAllDichVu()
        {
            try
            {
                using (var dataContext = new DataHotelLinQDataContext())
                {
                    var result = dataContext.DICHVUs.ToList();
                    return result;
                }

            }
            catch (Exception)
            {
                return null;
            }
        }

        public string InsertDichVu(DICHVU data)
        {
            try
            {
                using (var dataContext = new DataHotelLinQDataContext())
                {
                    //Check existed
                    var existItem = (from item in dataContext.DICHVUs where item.MaDichVu.Equals(data.MaDichVu) select item).FirstOrDefault();

                    var result = (from item in dataContext.DICHVUs
                                  orderby item.MaDichVu descending
                                  select item).FirstOrDefault();
                    if (result != null)
                    {
                        string id = result.MaDichVu;
                        data.MaDichVu = (int.Parse(id.Replace("DV", "")) + 1).ToString("DV0");
                    }
                    else
                        data.MaDichVu = "DV1";

                    dataContext.DICHVUs.InsertOnSubmit(data);
                    dataContext.SubmitChanges();
                    return "success";
                }
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }

        public string UpdateDichVu(DICHVU data)
        {
            try
            {
                using (var dataContext = new DataHotelLinQDataContext())
                {
                    var result = (from item in dataContext.DICHVUs
                                  where item.MaDichVu.Equals(data.MaDichVu)
                                  select item).SingleOrDefault();
                    result.DonGia = data.DonGia;
                    result.TenDichVu = data.TenDichVu;
                    dataContext.SubmitChanges();
                    return "success";
                }
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }

        public string DeleteDichVu(string id)
        {
            try
            {
                using (var dataContext = new DataHotelLinQDataContext())
                {
                    var result = (from item in dataContext.DICHVUs
                                  where item.MaDichVu.Equals(id)
                                  select item).SingleOrDefault();
                    dataContext.DICHVUs.DeleteOnSubmit(result);
                    dataContext.SubmitChanges();
                    return "success";
                }
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }

        public IEnumerable<DICHVU> GetByTopDichVu(string _top, string _where, string _order)
        {
            try
            {
                using (var data = new DataHotelLinQDataContext())
                {
                    string query = "Select * from [DICHVU]";
                    if (!_top.Equals(""))
                        query = " Select top " + _top + " * from [DICHVU] ";
                    if (!_where.Equals(""))
                        query += " where " + _where + " ";
                    if (!_order.Equals(""))
                        query += " order by " + _order;

                    IEnumerable<DICHVU> results = data.ExecuteQuery<DICHVU>(@query);

                    return results.ToList();
                }
            }
            catch (Exception)
            {
                return null;
            }
        }

        public string ApproveDichVu(DICHVU data)
        {
            try
            {
                using (var dataContext = new DataHotelLinQDataContext())
                {

                    //update LNTYPE
                    var result = (from current in dataContext.DICHVUs where current.MaDichVu == data.MaDichVu select current).Single();


                    dataContext.SubmitChanges();
                    return "success";
                }
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }

        #endregion
        #region [HOADON]

        public IEnumerable<HOADON> GetByAllHoaDon()
        {
            try
            {
                using (var dataContext = new DataHotelLinQDataContext())
                {
                    var result = dataContext.HOADONs.ToList();
                    return result;
                }

            }
            catch (Exception)
            {
                return null;
            }
        }

        public string InsertHoaDon(HOADON data)
        {
            try
            {
                using (var dataContext = new DataHotelLinQDataContext())
                {
                    //Check existed
                    var existItem = (from item in dataContext.HOADONs where item.SoHoaDon.Equals(data.SoHoaDon) select item).FirstOrDefault();

                    var result = (from item in dataContext.HOADONs
                                  orderby item.SoHoaDon descending
                                  select item).FirstOrDefault();
                    if (result != null)
                    {
                        string id = result.SoHoaDon;
                        data.SoHoaDon = (int.Parse(id.Replace("HD", "")) + 1).ToString("HD0");
                    }
                    else
                        data.SoHoaDon = "HD1";

                    dataContext.HOADONs.InsertOnSubmit(data);
                    dataContext.SubmitChanges();
                    return "success";
                }
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }

        public string UpdateHoaDon(HOADON data)
        {
            try
            {
                using (var dataContext = new DataHotelLinQDataContext())
                {
                    var result = (from item in dataContext.HOADONs
                                  where item.SoHoaDon.Equals(data.SoHoaDon)
                                  select item).SingleOrDefault();
                    result.DiaChi = data.DiaChi;
                    result.MaNhanVien = data.MaNhanVien;
                    result.NgayLap = data.NgayLap;
                    result.TenKhachHang = data.TenKhachHang;
                    result.TongTien = data.TongTien;
                    dataContext.SubmitChanges();
                    return "success";
                }
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }

        public string DeleteHoaDon(string id)
        {
            try
            {
                using (var dataContext = new DataHotelLinQDataContext())
                {
                    var result = (from item in dataContext.HOADONs
                                  where item.SoHoaDon.Equals(id)
                                  select item).SingleOrDefault();
                    dataContext.HOADONs.DeleteOnSubmit(result);
                    dataContext.SubmitChanges();
                    return "success";
                }
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }

        public IEnumerable<HOADON> GetByTopHoaDon(string _top, string _where, string _order)
        {
            try
            {
                using (var data = new DataHotelLinQDataContext())
                {
                    string query = "Select * from [HOADON]";
                    if (!_top.Equals(""))
                        query = " Select top " + _top + " * from [HOADON] ";
                    if (!_where.Equals(""))
                        query += " where " + _where + " ";
                    if (!_order.Equals(""))
                        query += " order by " + _order;

                    IEnumerable<HOADON> results = data.ExecuteQuery<HOADON>(@query);

                    return results.ToList();
                }
            }
            catch (Exception)
            {
                return null;
            }
        }

        public string ApproveHoaDon(HOADON data)
        {
            try
            {
                using (var dataContext = new DataHotelLinQDataContext())
                {

                    //update LNTYPE
                    var result = (from current in dataContext.HOADONs where current.SoHoaDon == data.SoHoaDon select current).Single();


                    dataContext.SubmitChanges();
                    return "success";
                }
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }

        #endregion       
        #region [LOAIKHACH]
        public IEnumerable<LOAIKHACH> GetByAllLoaiKhach()
        {
            try
            {
                using (var dataContext = new DataHotelLinQDataContext())
                {
                    var result = dataContext.LOAIKHACHes.ToList();
                    return result;
                }

            }
            catch (Exception)
            {
                return null;
            }
        }

        public string InsertLoaiKhach(LOAIKHACH data)
        {
            try
            {
                using (var dataContext = new DataHotelLinQDataContext())
                {
                    //Check existed
                    var existItem = (from item in dataContext.LOAIKHACHes where item.MaLoaiKhach.Equals(data.MaLoaiKhach) select item).FirstOrDefault();

                    var result = (from item in dataContext.LOAIKHACHes
                                  orderby item.MaLoaiKhach descending
                                  select item).FirstOrDefault();
                    if (result != null)
                    {
                        string id = result.MaLoaiKhach;
                        data.MaLoaiKhach = (int.Parse(id.Replace("LK", "")) + 1).ToString("LK0");
                    }
                    else
                        data.MaLoaiKhach = "LK1";

                    dataContext.LOAIKHACHes.InsertOnSubmit(data);
                    dataContext.SubmitChanges();
                    return "success";
                }
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }

        public string UpdateLoaiKhach(LOAIKHACH data)
        {
            try
            {
                using (var dataContext = new DataHotelLinQDataContext())
                {
                    var result = (from item in dataContext.LOAIKHACHes
                                  where item.MaLoaiKhach.Equals(data.MaLoaiKhach)
                                  select item).SingleOrDefault();
                    result.TenLoaiKhach = data.TenLoaiKhach;
                    dataContext.SubmitChanges();
                    return "success";
                }
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }

        public string DeleteLoaiKhach(string id)
        {
            try
            {
                using (var dataContext = new DataHotelLinQDataContext())
                {
                    var result = (from item in dataContext.LOAIKHACHes
                                  where item.MaLoaiKhach.Equals(id)
                                  select item).SingleOrDefault();
                    dataContext.LOAIKHACHes.DeleteOnSubmit(result);
                    dataContext.SubmitChanges();
                    return "success";
                }
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }

        public IEnumerable<LOAIKHACH> GetByTopLoaiKhach(string _top, string _where, string _order)
        {
            try
            {
                using (var data = new DataHotelLinQDataContext())
                {
                    string query = "Select * from [LOAIKHACH]";
                    if (!_top.Equals(""))
                        query = " Select top " + _top + " * from [LOAIKHACH] ";
                    if (!_where.Equals(""))
                        query += " where " + _where + " ";
                    if (!_order.Equals(""))
                        query += " order by " + _order;

                    IEnumerable<LOAIKHACH> results = data.ExecuteQuery<LOAIKHACH>(@query);

                    return results.ToList();
                }
            }
            catch (Exception)
            {
                return null;
            }
        }

        public string ApproveLoaiKhach(LOAIKHACH data)
        {
            try
            {
                using (var dataContext = new DataHotelLinQDataContext())
                {

                    //update LNTYPE
                    var result = (from current in dataContext.LOAIKHACHes where current.MaLoaiKhach == data.MaLoaiKhach select current).Single();


                    dataContext.SubmitChanges();
                    return "success";
                }
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }

        #endregion
        #region [LOAIPHONG]

        public IEnumerable<LOAIPHONG> GetByAllLoaiPhong()
        {
            try
            {
                using (var dataContext = new DataHotelLinQDataContext())
                {
                    var result = dataContext.LOAIPHONGs.ToList();
                    return result;
                }

            }
            catch (Exception)
            {
                return null;
            }
        }

        public string InsertLoaiPhong(LOAIPHONG data)
        {
            try
            {
                using (var dataContext = new DataHotelLinQDataContext())
                {
                    //Check existed
                    var existItem = (from item in dataContext.LOAIPHONGs where item.MaLoaiPhong.Equals(data.MaLoaiPhong) select item).FirstOrDefault();

                    var result = (from item in dataContext.LOAIPHONGs
                                  orderby item.MaLoaiPhong descending
                                  select item).FirstOrDefault();
                    if (result != null)
                    {
                        string id = result.MaLoaiPhong;
                        data.MaLoaiPhong = (int.Parse(id.Replace("LP", "")) + 1).ToString("LP0");
                    }
                    else
                        data.MaLoaiPhong = "LP1";

                    dataContext.LOAIPHONGs.InsertOnSubmit(data);
                    dataContext.SubmitChanges();
                    return "success";
                }
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }

        public string UpdateLoaiPhong(LOAIPHONG data)
        {
            try
            {
                using (var dataContext = new DataHotelLinQDataContext())
                {
                    var result = (from item in dataContext.LOAIPHONGs
                                  where item.MaLoaiPhong.Equals(data.MaLoaiPhong)
                                  select item).SingleOrDefault();
                    result.DonGia = data.DonGia;
                    result.TenLoaiPhong = data.TenLoaiPhong;
                    dataContext.SubmitChanges();
                    return "success";
                }
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }

        public string DeleteLoaiPhong(string id)
        {
            try
            {
                using (var dataContext = new DataHotelLinQDataContext())
                {
                    var result = (from item in dataContext.LOAIPHONGs
                                  where item.MaLoaiPhong.Equals(id)
                                  select item).SingleOrDefault();
                    dataContext.LOAIPHONGs.DeleteOnSubmit(result);
                    dataContext.SubmitChanges();
                    return "success";
                }
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }

        public IEnumerable<LOAIPHONG> GetByTopLoaiPhong(string _top, string _where, string _order)
        {
            try
            {
                using (var data = new DataHotelLinQDataContext())
                {
                    string query = "Select * from [LOAIPHONG]";
                    if (!_top.Equals(""))
                        query = " Select top " + _top + " * from [LOAIPHONG] ";
                    if (!_where.Equals(""))
                        query += " where " + _where + " ";
                    if (!_order.Equals(""))
                        query += " order by " + _order;

                    IEnumerable<LOAIPHONG> results = data.ExecuteQuery<LOAIPHONG>(@query);

                    return results.ToList();
                }
            }
            catch (Exception)
            {
                return null;
            }
        }

        public string ApproveLoaiPhong(LOAIPHONG data)
        {
            try
            {
                using (var dataContext = new DataHotelLinQDataContext())
                {

                    //update LNTYPE
                    var result = (from current in dataContext.LOAIPHONGs where current.MaLoaiPhong == data.MaLoaiPhong select current).Single();


                    dataContext.SubmitChanges();
                    return "success";
                }
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }

        #endregion
        #region [PHIEUTHUEPHONG]

        public IEnumerable<PHIEUTHUEPHONG> GetByAllPhieuThuePhong()
        {
            try
            {
                using (var dataContext = new DataHotelLinQDataContext())
                {
                    var result = dataContext.PHIEUTHUEPHONGs.ToList();
                    return result;
                }

            }
            catch (Exception)
            {
                return null;
            }
        }

        public string InsertPhieuThuePhong(PHIEUTHUEPHONG data)
        {
            try
            {
                using (var dataContext = new DataHotelLinQDataContext())
                {
                    //Check existed
                    var existItem = (from item in dataContext.PHIEUTHUEPHONGs where item.MaPhieuThue.Equals(data.MaPhieuThue) select item).FirstOrDefault();

                    var result = (from item in dataContext.PHIEUTHUEPHONGs
                                  orderby item.MaPhieuThue descending
                                  select item).FirstOrDefault();
                    if (result != null)
                    {
                        string id = result.MaPhieuThue;
                        data.MaPhieuThue = (int.Parse(id.Replace("PTP", "")) + 1).ToString("PTP0");
                    }
                    else
                        data.MaPhieuThue = "PTP1";

                    dataContext.PHIEUTHUEPHONGs.InsertOnSubmit(data);
                    dataContext.SubmitChanges();
                    return "success";
                }
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }

        public string UpdatePhieuThuePhong(PHIEUTHUEPHONG data)
        {
            try
            {
                using (var dataContext = new DataHotelLinQDataContext())
                {
                    var result = (from item in dataContext.PHIEUTHUEPHONGs
                                  where item.MaPhieuThue.Equals(data.MaPhieuThue)
                                  select item).SingleOrDefault();
                    result.DaThanhToan = data.DaThanhToan;
                    result.GioBatDauThue = data.GioBatDauThue;
                    result.MaHinhThucThue = data.MaHinhThucThue;
                    result.MaNhanVien = data.MaNhanVien;
                    result.MaPhong = data.MaPhong;
                    result.NgayThue = data.NgayThue;
                    result.SoKhach = data.SoKhach;
                    dataContext.SubmitChanges();
                    return "success";
                }
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }

        public string DeletePhieuThuePhong(string id)
        {
            try
            {
                using (var dataContext = new DataHotelLinQDataContext())
                {
                    var result = (from item in dataContext.PHIEUTHUEPHONGs
                                  where item.MaPhieuThue.Equals(id)
                                  select item).SingleOrDefault();
                    dataContext.PHIEUTHUEPHONGs.DeleteOnSubmit(result);
                    dataContext.SubmitChanges();
                    return "success";
                }
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }

        public IEnumerable<PHIEUTHUEPHONG> GetByTopPhieuThuePhong(string _top, string _where, string _order)
        {
            try
            {
                using (var data = new DataHotelLinQDataContext())
                {
                    string query = "Select * from [PHIEUTHUEPHONG]";
                    if (!_top.Equals(""))
                        query = " Select top " + _top + " * from [PHIEUTHUEPHONG] ";
                    if (!_where.Equals(""))
                        query += " where " + _where + " ";
                    if (!_order.Equals(""))
                        query += " order by " + _order;

                    IEnumerable<PHIEUTHUEPHONG> results = data.ExecuteQuery<PHIEUTHUEPHONG>(@query);

                    return results.ToList();
                }
            }
            catch (Exception)
            {
                return null;
            }
        }

        public string ApprovePhieuThuePhong(PHIEUTHUEPHONG data)
        {
            try
            {
                using (var dataContext = new DataHotelLinQDataContext())
                {

                    //update LNTYPE
                    var result = (from current in dataContext.PHIEUTHUEPHONGs where current.MaPhieuThue == data.MaPhieuThue select current).Single();


                    dataContext.SubmitChanges();
                    return "success";
                }
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }

        #endregion
        #region [PHONG]

        public IEnumerable<PHONG> GetByAllPhong()
        {
            try
            {
                using (var dataContext = new DataHotelLinQDataContext())
                {
                    var result = dataContext.PHONGs.ToList();
                    return result;
                }

            }
            catch (Exception)
            {
                return null;
            }
        }

        public string InsertPhong(PHONG data)
        {
            try
            {
                using (var dataContext = new DataHotelLinQDataContext())
                {
                    //Check existed
                    var existItem = (from item in dataContext.PHONGs where item.MaPhong.Equals(data.MaPhong) select item).FirstOrDefault();

                    var result = (from item in dataContext.PHONGs
                                  orderby item.MaPhong descending
                                  select item).FirstOrDefault();
                    if (result != null)
                    {
                        string id = result.MaPhong;
                        data.MaPhong = (int.Parse(id.Replace("P", "")) + 1).ToString("P0");
                    }
                    else
                        data.MaPhong = "P1";

                    dataContext.PHONGs.InsertOnSubmit(data);
                    dataContext.SubmitChanges();
                    return "success";
                }
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }

        public string UpdatePhong(PHONG data)
        {
            try
            {
                using (var dataContext = new DataHotelLinQDataContext())
                {
                    var result = (from item in dataContext.PHONGs
                                  where item.MaPhong.Equals(data.MaPhong)
                                  select item).SingleOrDefault();
                    result.Anh = data.Anh;
                    result.GhiChu = data.GhiChu;
                    result.MaLoaiPhong = data.MaLoaiPhong;
                    result.MoTa = data.MoTa;
                    result.TinhTrang = data.TinhTrang;
                    dataContext.SubmitChanges();
                    return "success";
                }
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }

        public string DeletePhong(string id)
        {
            try
            {
                using (var dataContext = new DataHotelLinQDataContext())
                {
                    var result = (from item in dataContext.PHONGs
                                  where item.MaPhong.Equals(id)
                                  select item).SingleOrDefault();
                    dataContext.PHONGs.DeleteOnSubmit(result);
                    dataContext.SubmitChanges();
                    return "success";
                }
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }

        public IEnumerable<PHONG> GetByTopPhong(string _top, string _where, string _order)
        {
            try
            {
                using (var data = new DataHotelLinQDataContext())
                {
                    string query = "Select * from [PHONG]";
                    if (!_top.Equals(""))
                        query = " Select top " + _top + " * from [PHONG] ";
                    if (!_where.Equals(""))
                        query += " where " + _where + " ";
                    if (!_order.Equals(""))
                        query += " order by " + _order;

                    IEnumerable<PHONG> results = data.ExecuteQuery<PHONG>(@query);

                    return results.ToList();
                }
            }
            catch (Exception)
            {
                return null;
            }
        }

        public string ApprovePhong(PHONG data)
        {
            try
            {
                using (var dataContext = new DataHotelLinQDataContext())
                {

                    //update LNTYPE
                    var result = (from current in dataContext.PHONGs where current.MaPhong == data.MaPhong select current).Single();


                    dataContext.SubmitChanges();
                    return "success";
                }
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }

        #endregion
        #region [PHONGLAMVIEC]

        public IEnumerable<PHONGLAMVIEC> GetByAllPhongLamViec()
        {
            try
            {
                using (var dataContext = new DataHotelLinQDataContext())
                {
                    var result = dataContext.PHONGLAMVIECs.ToList();
                    return result;
                }

            }
            catch (Exception)
            {
                return null;
            }
        }

        public string InsertPhongLamViec(PHONGLAMVIEC data)
        {
            try
            {
                using (var dataContext = new DataHotelLinQDataContext())
                {
                    //Check existed
                    var existItem = (from item in dataContext.PHONGLAMVIECs where item.MaPhong.Equals(data.MaPhong) select item).FirstOrDefault();

                    var result = (from item in dataContext.PHONGLAMVIECs
                                  orderby item.MaPhong descending
                                  select item).FirstOrDefault();
                    if (result != null)
                    {
                        string id = result.MaPhong;
                        data.MaPhong = (int.Parse(id.Replace("PLV", "")) + 1).ToString("PLV0");
                    }
                    else
                        data.MaPhong = "PLV0";

                    dataContext.PHONGLAMVIECs.InsertOnSubmit(data);
                    dataContext.SubmitChanges();
                    return "success";
                }
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }

        public string UpdatePhongLamViec(PHONGLAMVIEC data)
        {
            try
            {
                using (var dataContext = new DataHotelLinQDataContext())
                {
                    var result = (from item in dataContext.PHONGLAMVIECs
                                  where item.MaPhong.Equals(data.MaPhong)
                                  select item).SingleOrDefault();
                    result.DiaDiem = data.DiaDiem;
                    result.MaPhong = data.MaPhong;
                    dataContext.SubmitChanges();
                    return "success";
                }
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }

        public string DeletePhongLamViec(string id)
        {
            try
            {
                using (var dataContext = new DataHotelLinQDataContext())
                {
                    var result = (from item in dataContext.PHONGLAMVIECs
                                  where item.MaPhong.Equals(id)
                                  select item).SingleOrDefault();
                    dataContext.PHONGLAMVIECs.DeleteOnSubmit(result);
                    dataContext.SubmitChanges();
                    return "success";
                }
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }

        public IEnumerable<PHONGLAMVIEC> GetByTopPhongLamViec(string _top, string _where, string _order)
        {
            try
            {
                using (var data = new DataHotelLinQDataContext())
                {
                    string query = "Select * from [PHONGLAMVIEC]";
                    if (!_top.Equals(""))
                        query = " Select top " + _top + " * from [PHONGLAMVIEC] ";
                    if (!_where.Equals(""))
                        query += " where " + _where + " ";
                    if (!_order.Equals(""))
                        query += " order by " + _order;

                    IEnumerable<PHONGLAMVIEC> results = data.ExecuteQuery<PHONGLAMVIEC>(@query);

                    return results.ToList();
                }
            }
            catch (Exception)
            {
                return null;
            }
        }

        public string ApprovePhongLamViec(PHONGLAMVIEC data)
        {
            try
            {
                using (var dataContext = new DataHotelLinQDataContext())
                {

                    //update LNTYPE
                    var result = (from current in dataContext.PHONGLAMVIECs where current.MaPhong == data.MaPhong select current).Single();


                    dataContext.SubmitChanges();
                    return "success";
                }
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }

        #endregion
        #region [PHUCAP]

        public IEnumerable<PHUCAP> GetByAllPhuCap()
        {
            try
            {
                using (var dataContext = new DataHotelLinQDataContext())
                {
                    var result = dataContext.PHUCAPs.ToList();
                    return result;
                }

            }
            catch (Exception)
            {
                return null;
            }
        }

        public string InsertPhuCap(PHUCAP data)
        {
            try
            {
                using (var dataContext = new DataHotelLinQDataContext())
                {
                    //Check existed
                    var existItem = (from item in dataContext.PHUCAPs where item.MaPhuCap.Equals(data.MaPhuCap) select item).FirstOrDefault();

                    var result = (from item in dataContext.PHUCAPs
                                  orderby item.MaPhuCap descending
                                  select item).FirstOrDefault();
                    if (result != null)
                    {
                        string id = result.MaPhuCap;
                        data.MaPhuCap = (int.Parse(id.Replace("PC", "")) + 1).ToString("PC");
                    }
                    else
                        data.MaPhuCap = "PC";

                    dataContext.PHUCAPs.InsertOnSubmit(data);
                    dataContext.SubmitChanges();
                    return "success";
                }
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }

        public string UpdatePhuCap(PHUCAP data)
        {
            try
            {
                using (var dataContext = new DataHotelLinQDataContext())
                {
                    var result = (from item in dataContext.PHUCAPs
                                  where item.MaPhuCap.Equals(data.MaPhuCap)
                                  select item).SingleOrDefault();
                    result.HeSoPhuCap = data.HeSoPhuCap;
                    result.MaPhuCap = data.MaPhuCap;
                    dataContext.SubmitChanges();
                    return "success";
                }
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }

        public string DeletePhuCap(string id)
        {
            try
            {
                using (var dataContext = new DataHotelLinQDataContext())
                {
                    var result = (from item in dataContext.PHUCAPs
                                  where item.MaPhuCap.Equals(id)
                                  select item).SingleOrDefault();
                    dataContext.PHUCAPs.DeleteOnSubmit(result);
                    dataContext.SubmitChanges();
                    return "success";
                }
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }

        public IEnumerable<PHUCAP> GetByTopPhuCap(string _top, string _where, string _order)
        {
            try
            {
                using (var data = new DataHotelLinQDataContext())
                {
                    string query = "Select * from [PHUCAP]";
                    if (!_top.Equals(""))
                        query = " Select top " + _top + " * from [PHUCAP] ";
                    if (!_where.Equals(""))
                        query += " where " + _where + " ";
                    if (!_order.Equals(""))
                        query += " order by " + _order;

                    IEnumerable<PHUCAP> results = data.ExecuteQuery<PHUCAP>(@query);

                    return results.ToList();
                }
            }
            catch (Exception)
            {
                return null;
            }
        }

        public string ApprovePhuCap(PHUCAP data)
        {
            try
            {
                using (var dataContext = new DataHotelLinQDataContext())
                {

                    //update LNTYPE
                    var result = (from current in dataContext.PHUCAPs where current.MaPhuCap == data.MaPhuCap select current).Single();


                    dataContext.SubmitChanges();
                    return "success";
                }
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }

        #endregion
        #region [THAMSO]

        public IEnumerable<THAMSO> GetByAllThamSo()
        {
            try
            {
                using (var dataContext = new DataHotelLinQDataContext())
                {
                    var result = dataContext.THAMSOs.ToList();
                    return result;
                }

            }
            catch (Exception)
            {
                return null;
            }
        }

        public string InsertThamSo(THAMSO data)
        {
            try
            {
                using (var dataContext = new DataHotelLinQDataContext())
                {
                    //Check existed
                    var existItem = (from item in dataContext.THAMSOs where item.TenThamSo.Equals(data.TenThamSo) select item).FirstOrDefault();

                    var result = (from item in dataContext.THAMSOs
                                  orderby item.TenThamSo descending
                                  select item).FirstOrDefault();
                    if (result != null)
                    {
                       
                    }
                    else
                       

                    dataContext.THAMSOs.InsertOnSubmit(data);
                    dataContext.SubmitChanges();
                    return "success";
                }
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }

        public string UpdateThamSo(THAMSO data)
        {
            try
            {
                using (var dataContext = new DataHotelLinQDataContext())
                {
                    var result = (from item in dataContext.THAMSOs
                                  where item.TenThamSo.Equals(data.TenThamSo)
                                  select item).SingleOrDefault();
                    result.GiaTri = data.GiaTri;
                    result.TenThamSo = data.TenThamSo;
                    dataContext.SubmitChanges();
                    return "success";
                }
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }

        public string DeleteThamSo(string id)
        {
            try
            {
                using (var dataContext = new DataHotelLinQDataContext())
                {
                    var result = (from item in dataContext.THAMSOs
                                  where item.TenThamSo.Equals(id)
                                  select item).SingleOrDefault();
                    dataContext.THAMSOs.DeleteOnSubmit(result);
                    dataContext.SubmitChanges();
                    return "success";
                }
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }

        public IEnumerable<THAMSO> GetByTopThamSo(string _top, string _where, string _order)
        {
            try
            {
                using (var data = new DataHotelLinQDataContext())
                {
                    string query = "Select * from [THAMSO]";
                    if (!_top.Equals(""))
                        query = " Select top " + _top + " * from [THAMSO] ";
                    if (!_where.Equals(""))
                        query += " where " + _where + " ";
                    if (!_order.Equals(""))
                        query += " order by " + _order;

                    IEnumerable<THAMSO> results = data.ExecuteQuery<THAMSO>(@query);

                    return results.ToList();
                }
            }
            catch (Exception)
            {
                return null;
            }
        }

        public string ApproveThamSo(THAMSO data)
        {
            try
            {
                using (var dataContext = new DataHotelLinQDataContext())
                {

                    //update LNTYPE
                    var result = (from current in dataContext.THAMSOs where current.TenThamSo == data.TenThamSo select current).Single();


                    dataContext.SubmitChanges();
                    return "success";
                }
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }

        #endregion
    }
}
